# -*- coding:utf-8 -*-

"""
@IDE     ：PyCharm
@Author  ：kiway
@Date    ：2023/12/12 22:28
"""
import json
import sys
import uuid
import threading


def get_uuid1():
    """Generate a UUID based on the host ID and current time

    Returns:
        s: UUID1 string.
    """
    uid1 = uuid.uuid1()
    s = str(uid1)
    return s


def get_uuid3(str_in):
    """Generate a UUID using an MD5 hash of a namespace UUID and a name

    Args:
        str_in: Input string.

    Returns:
        s: UUID3 string.
    """
    uid3 = uuid.uuid3(uuid.NAMESPACE_DNS, str_in)
    s = str(uid3)
    return s


def get_uuid4():
    """Generate a random UUID.

    Returns:
        s: UUID5 string.
    """
    uid4 = uuid.uuid4()
    s = str(uid4)
    return s


def get_uuid5(str_in):
    """Generate a UUID using a SHA-1 hash of a namespace UUID and a name

    Args:
        str_in: Input string.

    Returns:
        s: UUID5 string.
    """
    uid5 = uuid.uuid5(uuid.NAMESPACE_DNS, str_in)
    s = str(uid5)
    return s


class Timer:
    """自定一个定时器"""

    def __init__(self, cross_timeout_interval, event_flag):
        self.event_flag = event_flag
        self.cross_timeout_interval = cross_timeout_interval
        self.__tm: threading.Timer = None

    def change_state(self):
        self.event_flag.set()

    def start(self):
        self.__tm = threading.Timer(interval=self.cross_timeout_interval,
                                    function=self.change_state)

        self.__tm.start()

    def handle_exception(self, type_, value, traceback):
        print("timeout 测试")

    def cancel(self):
        self.__tm.cancel()


def load_json(file_name) -> dict:
    with open(file_name, 'r') as f:
        file_dict = json.load(f)

    return file_dict


def get_base_symbol_name(symbol_name):
    return symbol_name.split("/USDT")[0]


def split_list(lst, n):
    # 如果子列表数大于列表长度，确保每个子列表至少有一个元素
    if n >= len(lst):
        return [[elem] for elem in lst]

    # 否则，按均匀分配的方式切分列表
    size = len(lst) // n  # 每个子列表的基础大小
    remainder = len(lst) % n  # 余数，用来分配给前几个子列表

    result = []
    i = 0
    for i in range(n):
        # 如果还有余数，则当前子列表会有一个额外的元素
        extra = 1 if i < remainder else 0
        result.append(lst[i * size + min(i, remainder): (i + 1) * size + min(i + 1, remainder)])

    # 过滤掉空的子列表
    return [sublist for sublist in result if len(sublist) != 0]
